<?php
namespace JsonRpc\Services\System;
use JsonRpc\Models\SysPermission;
use JsonRpc\Models\SysUser;
use JsonRpc\Services\CommonService;
use JsonRpc\Utils\Utils;

/**
 *  用户
 * @author walkor <worker-man@qq.com>
 */
class UserService
{
    use CommonService;

    private static function getBaseModel(){
        return "JsonRpc\\Models\\SysUser";
    }

    //用户登录
    public static function userLogin($params)
    {
        if(empty($params)){
            throw new \Exception('用户名或密码不能为空', 400);
        }
        $user = SysUser::where('username',$params['username'])->first();
        if ($user && $user->is_active && Utils::encryptVerifiy($params['password'], $user->password)){
            //用户账号验证通过
            return $user->toArray();
        }else{
            return false;
        }
    }

    /**
     * 获取用户菜单
     * @param $params
     */
    public static function getUserMenu($params){
        /*$menus['menus'] = [
            [
                "menuid"=>"1","icon"=>"icon-add","menuname"=>"系统",
                "menus"=>[
                    [
                        "menuid"=>"2","icon"=>"icon-add","menuname"=>"基础信息",
                        "menus"=>[

                            ["menuid"=>"22","menuname"=>"帐套管理","icon"=>"icon-add","url"=>"/sys/client"],
                            ["menuid"=>"23","menuname"=>"组织机构管理","icon"=>"icon-add","url"=>"/sys/org"],
                            ["menuid"=>"24","menuname"=>"数据字典","icon"=>"icon-add","url"=>"/sys/dict"],
                            ["menuid"=>"24","menuname"=>"期间","icon"=>"icon-add","url"=>"/sys/period"],

                        ]
                    ],
                    [
                        "menuid"=>"2","icon"=>"icon-add","menuname"=>"产品信息",
                        "menus"=>[
                            ["menuid"=>"22","menuname"=>"品牌管理","icon"=>"icon-add","url"=>"/product/brand"],
                            ["menuid"=>"23","menuname"=>"分类管理","icon"=>"icon-add","url"=>"/product/category"],
                            ["menuid"=>"23","menuname"=>"标签管理","icon"=>"icon-add","url"=>"/product/label"],
                            ["menuid"=>"23","menuname"=>"产品管理","icon"=>"icon-add","url"=>"/product/goods"],
                        ]
                    ],
                    [
                        "menuid"=>"2","icon"=>"icon-add","menuname"=>"客户管理",
                        "menus"=>[
                            ["menuid"=>"23","menuname"=>"新增客户","icon"=>"icon-add","url"=>"/product/category"],
                            ["menuid"=>"22","menuname"=>"客户列表","icon"=>"icon-add","url"=>"/product/brand"],
                        ]
                    ],
                    [
                        "menuid"=>"2","icon"=>"icon-add","menuname"=>"供应商管理",
                        "menus"=>[
                            ["menuid"=>"22","menuname"=>"新增供应商","icon"=>"icon-add","url"=>"/product/brand"],
                            ["menuid"=>"23","menuname"=>"供应商列表","icon"=>"icon-add","url"=>"/product/category"]
                        ]
                    ],
                    [
                        "menuid"=>"2","icon"=>"icon-add","menuname"=>"权限管理",
                        "menus"=>[
                            ["menuid"=>"27","menuname"=>"功能按钮","icon"=>"icon-add","url"=>"/sys/button"],
                            ["menuid"=>"24","menuname"=>"菜单管理","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"22","menuname"=>"用户管理","icon"=>"icon-add","url"=>"/sys/user"],
                            ["menuid"=>"23","menuname"=>"角色管理","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"23","menuname"=>"权限编码","icon"=>"icon-add","url"=>"/sys/menu"],
                        ]
                    ]
                ]
            ],
            [
                "menuid"=>"20","icon"=>"icon-add","menuname"=>"销售",
                "menus"=>[
                    [
                        "menuid"=>"2","icon"=>"icon-standard-house","menuname"=>"求/报价管理",
                        "menus"=>[
                            ["menuid"=>"22","menuname"=>"待报价","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"22","menuname"=>"历史求购","icon"=>"icon-add","url"=>"/sys/menu"],
                        ]
                    ],
                    [
                        "menuid"=>"2","icon"=>"icon-standard-house","menuname"=>"销售管理",
                        "menus"=>[
                            ["menuid"=>"23","menuname"=>"认购单","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"23","menuname"=>"待转订单","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"24","menuname"=>"销售订单","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"25","menuname"=>"销售明细报表","icon"=>"icon-add","url"=>"/sys/menu"]
                        ]
                    ],
                ]
            ],
            [
                "menuid"=>"30","icon"=>"icon-add","menuname"=>"采购",
                "menus"=>[
                    [
                        "menuid"=>"2","icon"=>"icon-standard-house","menuname"=>"采购管理",
                        "menus"=>[
                            ["menuid"=>"24","menuname"=>"认购转采","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"22","menuname"=>"采购备库","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"22","menuname"=>"采购订单","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"23","menuname"=>"采购明细报表","icon"=>"icon-add","url"=>"/sys/menu"]
                        ]
                    ]
                ]
            ],
            [
                "menuid"=>"40","icon"=>"icon-add","menuname"=>"仓储物流",
                "menus"=>[
                    [
                        "menuid"=>"2","icon"=>"icon-standard-house","menuname"=>"仓储管理",
                        "menus"=>[
                            ["menuid"=>"22","menuname"=>"仓库列表","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"23","menuname"=>"现有库存","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"24","menuname"=>"库存调拨","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"25","menuname"=>"库存盘损","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"25","menuname"=>"现货盘盈","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"25","menuname"=>"串号跟踪","icon"=>"icon-add","url"=>"/sys/menu"],
                        ]
                    ],
                    [
                        "menuid"=>"2","icon"=>"icon-standard-house","menuname"=>"出入库管理",
                        "menus"=>[
                            ["menuid"=>"22","menuname"=>"待入订单","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"23","menuname"=>"入库单","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"24","menuname"=>"待出订单","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"25","menuname"=>"出库单","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"25","menuname"=>"待确认收货","icon"=>"icon-add","url"=>"/sys/menu"],
                        ]
                    ],
                ]
            ],
            [
                "menuid"=>"50","icon"=>"icon-add","menuname"=>"财务",
                "menus"=>[
                    [
                        "menuid"=>"2","icon"=>"icon-standard-house","menuname"=>"财务管理",
                        "menus"=>[
                            ["menuid"=>"22","menuname"=>"销售发票","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"23","menuname"=>"采购发票","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"24","menuname"=>"销售收款","icon"=>"icon-add","url"=>"/sys/menu"],
                            ["menuid"=>"25","menuname"=>"采购付款","icon"=>"icon-add","url"=>"/sys/menu"]
                        ]
                    ]
                ]
            ]
        ];*/
        $menus = MenuService::listTreeGridData();

        return $menus;
    }

    //获取用户列表
    public static function getUserList($params)
    {
        $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
        if (isset($params['org_id'])) {
            $res = SysUser::where('client_id', $user['client_id'])->where('org_id',$params['org_id'])->get();
        }else{
            $res = SysUser::where('client_id', $user['client_id'])->get();
        }
        if (!empty($res)) {
            return $res;
        }
        return [];
    }

    //分类更新
    public function saveUserInfo($params)
    {
        if(isset($params['id']) && $params['id']){
            //更新
            $model = SysUser::find($params['id']);
            $model->updated_by = $params['curr_user_id'];
        }else{
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            //新增
            unset($params['id']);
            $model = new SysUser();
            $model->client_id = $user['client_id'];
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
        }
        $model->org_id = $params['org_id'];
        $model->sex = $params['sex'];
        $model->username = $params['username'];
        $model->realname = $params['realname'];
        $model->password = Utils::encrypt('888888');
        $model->qq = $params['qq'];
        $model->email = $params['email'];
        $model->fax = $params['fax'];
        $model->photo = $params['photo'];
        $model->remark = $params['remark'];
        $model->is_admin = $params['is_admin'];
        $model->is_active = $params['is_active'];

        return $model->save();

    }

    //冻结用户
    public static function remove($params)
    {
        if (!empty($params['id'])) {
            $res = SysUser::where('id',$params['id'])->update([
                'is_active' => '0'
            ]);
            return $res;
        }
        return [];
    }

    /**
     * 获取用户在某菜单上的操作权限
     * @param $params
     * @return array
     */
    public static function getMenuOperation($params){
        return ButtonService::getInfoList($params);
    }

    /**
     * 获取用户在某菜单上的数据权限
     * @param $params
     */
    public static function getMenuDataRule($params){
        $menu_id = $params['menu_id'];
        $user_id = $params['user_id'];
        return  [];
    }

}
